package com.ruitu.health_2.mvp.model;

import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseModel;
import com.ruitu.health_2.dao.http.APIRetrofit;
import com.ruitu.router_module.bean.BaseResult2;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.home.AdvertContent;
import com.ruitu.router_module.bean.product.Video;
import com.ruitu.router_module.bean.product.VideoDetial;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

public class VideoModel {

    public static class VideoListM extends BaseModel {

        public Observable<BaseResult2<PageModel2<Video>>> reqVideoList(int size, int num) {
            return APIRetrofit.getDefault()
                    .reqVideoList(size, num)
                    .compose(RxUtil.<BaseResult2<PageModel2<Video>>>subIoObMain());
        }

        public Observable<BaseResult2<PageModel2<Video>>> reqVideoListByTitle(int size, int num, String title) {
            return APIRetrofit.getDefault()
                    .reqVideoListByTitle(size, num, title)
                    .compose(RxUtil.<BaseResult2<PageModel2<Video>>>subIoObMain());
        }

        public Observable<BaseResult2<PageModel2<Video>>> reqVideoListByColumn(String posId, int size, int num) {
            return APIRetrofit.getDefault()
                    .reqVideoListByTab(posId, size, num)
                    .map(new Function<BaseResult2<PageModel2<AdvertContent<Video>>>, BaseResult2<PageModel2<Video>>>() {
                        @Override
                        public BaseResult2<PageModel2<Video>> apply(BaseResult2<PageModel2<AdvertContent<Video>>> pageModel2BaseResult2) throws Exception {
                            BaseResult2<PageModel2<Video>> baseResult2 = new BaseResult2<>();
                            baseResult2.setCode(pageModel2BaseResult2.getCode());
                            baseResult2.setMessage(pageModel2BaseResult2.getMessage());
                            List<Video> readList = new ArrayList<>();
                            for (int i = 0; i < pageModel2BaseResult2.getData().getList().size(); i++) {
                                readList.add(pageModel2BaseResult2.getData().getList().get(i).getInfoObject());
                            }
                            baseResult2.getData().setList(readList);
                            return baseResult2;
                        }
                    })
                    .compose(RxUtil.<BaseResult2<PageModel2<Video>>>subIoObMain());
        }
    }

    public static class VideoDetailM extends BaseModel {
        public Observable<BaseResult2<VideoDetial>> reqVideoDetail(String id, String memberId) {
            return APIRetrofit.getDefault()
                    .reqVideoDetail(id, memberId)
                    .compose(RxUtil.<BaseResult2<VideoDetial>>subIoObMain());
        }
    }
}
